package com.lovo.common.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.checkerframework.checker.units.qual.C;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
//@Configuration
public class FeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(attributes != null){

            HttpServletRequest request = attributes.getRequest();
            //添加token
            requestTemplate.header("token", request.getHeader("token"));
        }else{
            // 如果是微服务间的调用，则绕过认证
            requestTemplate.header("token","none");
        }
    }
}
